﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Tools.Linq;
using MobiMenu.Restaurant.Web.Utility;
using System.Collections.Specialized;
using System.Linq.Expressions;

namespace MobiMenu.Restaurant.Web
{
    /// <summary>
    ///Class1 的摘要说明
    /// </summary>
    public class OrdersService : BaseRESTService<Order>
    {
        public OrdersService()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

        protected override Expression<Func<Order, bool>> GetWhereExpression(NameValueCollection nameValues)
        {
            var queryBuilder = QueryBuilder.Create<Order>();
            
            int[] ids;
            if (RequestHelper.GetInt32Array(nameValues, "Id", out ids))
            {
                queryBuilder = queryBuilder.In(c => c.Id, ids);
            }

            DateTime startTime;
            DateTime endTime;
            if (RequestHelper.GetDateTimeRange(nameValues, "OrderedTime", out startTime, out endTime))
            {
                queryBuilder = queryBuilder.Between(c => c.OrderedTime, startTime, endTime);            
            }

            string[] tableIds;
            if (RequestHelper.GetStringArray(nameValues, "TableId", out tableIds))
            {
                queryBuilder = queryBuilder.In(c => c.TableId, tableIds);
            }

            string[] waiterIds;
            if (RequestHelper.GetStringArray(nameValues, "WaiterId", out waiterIds))
            {
                queryBuilder = queryBuilder.In(c => c.WaiterId, waiterIds);
            }

            OrderStatus[] orderStatuses;
            if (RequestHelper.GetEnumArray<OrderStatus>(nameValues, "Status", out orderStatuses))
            {
                queryBuilder = queryBuilder.In(c => c.Status, orderStatuses);
            }

            return queryBuilder.Expression;
        }

        protected override bool ProcessOrder(string sortField, bool isDesc, ref IQueryable<Order> query)
        {
            if (string.IsNullOrEmpty(sortField))
            {
                return false;
            }

            if (sortField.Equals("Id", StringComparison.InvariantCultureIgnoreCase))
            {
                Expression<Func<Order, int>> expr = c => c.Id;
                query = CommonHelper.QueryOrder<Order, int>(query, expr, isDesc);
                return true;
            }

            if (sortField.Equals("OrderedTime", StringComparison.InvariantCultureIgnoreCase))
            {
                Expression<Func<Order, DateTime>> expr = c => c.OrderedTime;
                query = CommonHelper.QueryOrder<Order, DateTime>(query, expr, isDesc);
                return true;
            }

            if (sortField.Equals("Status", StringComparison.InvariantCultureIgnoreCase))
            {
                Expression<Func<Order, OrderStatus>> expr = c => c.Status;
                query = CommonHelper.QueryOrder<Order, OrderStatus>(query, expr, isDesc);
                return true;
            }

            if (sortField.Equals("TableId", StringComparison.InvariantCultureIgnoreCase))
            {
                Expression<Func<Order, string>> expr = c => c.TableId;
                query = CommonHelper.QueryOrder<Order, string>(query, expr, isDesc);
                return true;
            }

            if (sortField.Equals("WaiterId", StringComparison.InvariantCultureIgnoreCase))
            {
                Expression<Func<Order, string>> expr = c => c.WaiterId;
                query = CommonHelper.QueryOrder<Order, string>(query, expr, isDesc);
                return true;
            }

            return false;
        }

        protected override bool GetDeleteWhereExpression(RESTContext<Order> context,
            out Expression<Func<Order, bool>> expr)
        {
            expr = null;
            if (context == null || context.RequestData == null
                || context.RequestData.DeleteIds == null || context.RequestData.DeleteIds.Count == 0)
            {
                return false;
            }

            var queryBuilder = QueryBuilder.Create<Order>();
            queryBuilder = queryBuilder.In(c => c.Id, context.RequestData.DeleteIds.ToArray());
            expr = queryBuilder.Expression;
            return true;
        }

        protected override bool UpdateEntity(NameValueCollection nameValues, Order item)
        {
            bool changed = false;
            DateTime dt;
            if (RequestHelper.GetDateTime(nameValues, "OrderedTime", out dt))
            {
                item.OrderedTime = dt;
                changed = true;
            }

            string tableId;
            if (RequestHelper.GetString(nameValues, "TableId", out tableId))
            {
                item.TableId = tableId;
                changed = true;
            }

            string waiterId;
            if (RequestHelper.GetString(nameValues, "WaiterId", out waiterId))
            {
                item.WaiterId = waiterId;
                changed = true;
            }

            OrderStatus orderStatus;
            if (RequestHelper.GetEnum<OrderStatus>(nameValues, "Status", out orderStatus))
            {
                item.Status = orderStatus;
                changed = true;
            }

            return changed;
        }

        protected override bool GetUpdateWhereExpression(NameValueCollection nameValues, out Expression<Func<Order, bool>> expr)
        {
            var queryBuilder = QueryBuilder.Create<Order>();
            int id;
            if (RequestHelper.GetInt32(nameValues, "Id", out id))
            {
                queryBuilder = queryBuilder.Equals(c => c.Id, id);
                expr = queryBuilder.Expression;
                return true;
            }

            expr = null;
            return false;
        }
    }
}